package com.steven.ourcontest.utils

import java.text.SimpleDateFormat
import java.util.*

object DateUtil {
    /**
     * 时间戳转换成日期格式字符串
     * @param seconds 精确到秒的字符串
     * @param formatStr
     * @return
     */
    fun timeStamp2Date(ms: Long?, format: String?): String {
        var format = format
        if (ms == null) {
            return ""
        }
        if (format == null || format.isEmpty()) {
            format = "yyyy-MM-dd HH:mm:ss"
        }
        val sdf = SimpleDateFormat(format)
        return sdf.format(Date(ms))
    }

    /**
     * 日期格式字符串转换成时间戳
     * @param date 字符串日期
     * @param format 如：yyyy-MM-dd HH:mm:ss
     * @return
     */
    fun date2TimeStamp(date_str: String?, format: String?): String {
        try {
            val sdf = SimpleDateFormat(format)
            return (sdf.parse(date_str).time / 1000).toString()
        } catch (e: Exception) {
            e.printStackTrace()
        }
        return ""
    }
}